#include "PayManager.h"

#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include "Misc.h"

USING_NS_CC;

extern "C" {


	void buyProduct_c(const char* productId, int nCount)
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/PayManager", "buyProduct", "(Ljava/lang/String;I)V")) {

			jstring stringArg1 = t.env->NewStringUTF(productId);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, nCount);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(t.classID);
		}
	}

	void askPayDialog_c(const char* msg, int nCount)
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/PayManager", "askPayDialog", "(Ljava/lang/String;I)V")) {

			jstring stringArg1 = t.env->NewStringUTF(msg);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, nCount);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(t.classID);
		}
	}

	void requestOrderform_c(const char* formstr, int nCount)
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/PayManager", "requestOrderform", "(Ljava/lang/String;I)V")) {

			jstring stringArg1 = t.env->NewStringUTF(formstr);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, nCount);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(t.classID);
		}
	}
}

//=========================================

PayManager* PayManager::sharedPayManager()
{
	static PayManager s_payManager;
	return &s_payManager;
}

PayManager::PayManager()
: m_bPaying(false)
{
	
}

PayManager::~PayManager()
{

}


void PayManager::buyProduct(const char* productId, int nCount)
{
	buyProduct_c(productId, nCount);
}

void PayManager::payed()
{
	m_bPaying = false;
}

bool PayManager::isPaying()
{
	return m_bPaying;
}

void PayManager::askPayDialog(const char* msg, int nCount)
{
	askPayDialog_c(msg, nCount);
	m_bPaying = true;
}

void PayManager::payFailed()
{
	payed();
}

void PayManager::requestOrderform(const std::string& formstr, int nCount)
{
	requestOrderform_c(formstr.c_str(), nCount);
	m_bPaying = true;
}